package cn.tedu.mall.order.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.order.service.impl.OmsCartServiceImpl;
import cn.tedu.mall.order.utils.WebConsts;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author BJ
 * @date 2022/11/3 19:12
 */
@RestController
@RequestMapping("/oms/cart")
@Api(tags = "购物车模块")
public class CartController {
    @Autowired
    private IOmsCartService omsCartService;

    @PostMapping("/add")
    @ApiOperation("向购物车中添加sku信息")
    @PreAuthorize("hasAuthority('ROLE_user')")
    public JsonResult addCards(@Validated CartAddDTO cartAddDTO) {
        omsCartService.addCart(cartAddDTO);
        return JsonResult.ok("添加购物车成功！");
    }

    @GetMapping("/list")
    @ApiOperation("分页显示购物车中sku商品信息")
    @PreAuthorize("hasAuthority('ROLE_user')")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",example = "10")
    })
    public JsonResult<JsonPage<CartStandardVO>> page(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize
    ){
        JsonPage<CartStandardVO> jsonPage = omsCartService.listCarts(page, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @PostMapping("/delete")
    @ApiOperation("根据商品id批量删除购物车中的sku信息")
    @PreAuthorize("hasAuthority('ROLE_user')")
    public JsonResult deleteCards(Long[] ids) {
        omsCartService.removeCart(ids);
        return JsonResult.ok("删除购物车中sku信息成功！");
    }

    @PostMapping("/all")
    @ApiOperation("清空购物车")
    @PreAuthorize("hasRole('user')")
    public JsonResult removeAllCarts(){
        omsCartService.removeAllCarts();
        return JsonResult.ok("购物车已清空！");
    }

    @PostMapping("/update")
    @ApiOperation("修改购物车中sku的数量")
    @PreAuthorize("hasRole('user')")
    public JsonResult updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        omsCartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("修改购物车中商品的数量成功");
    }
}
